package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.ErrorCodes;
import unipi.database.PrivateMessage;
import unipi.database.User;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;
import unipi.helpers.Validator;

public class SendMessage extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public SendMessage()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}	
	
	private String getPage(String uid, String recipient, String message)
	{
		String content = "<h1>Αποστολή Μηνύματος</h1>";
		String to = "δεν ειναι διαθέσιμο";
		
		Map<String, String> map = User.getUserData(recipient);
		if(map != null)
			to = map.get("surname") + " " + map.get("name");
		
		content += HTMLPages.getSubmitForm("SendMessage", 
							new String[]
									{
										"",
										"Πρός",
										"Τίτλος",
										"Περιεχόμενο"				
									}, 
							new String[]
									{
										"<input class=\"mediumInput\" name=\"to\" type=\"hidden\" value=\"" + recipient + "\" />",
										to,
										"<input class=\"mediumInput\" name=\"title\" type=\"text\" value=\"\" />",
										"<textarea  class=\"txtarea\" name=\"body\" rows=\"6\" cols=\"40\" ></textarea>"				
									}, "Αποστολή") + message;
		
		return HTMLPages.getGeneralPage(content, uid);	
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			request.setCharacterEncoding("utf-8");
			String title = request.getParameter("title");
			String body = request.getParameter("body");
			String recipientID = request.getParameter("to");
			
			if(title != null && body != null && recipientID != null)
			{
				String message = "";
				
				title = Validator.escape(title);
				body = Validator.escape(body);
				
				if(!Validator.isNumber(recipientID))
				{
					message = "Ο χρήστης αυτός δεν υπάρχει.";
				}
				else
				{
					if(body.equals(""))
					{
						message = "Δεν μπορείτε να στείλετε κενό μήνυμα.";
					}
					else if(title.equals(""))
					{
						message = "Δεν έχετε γράψει θέμα στο μήνυμα σας.";
					}
					else if(!User.exists(recipientID))
						message = "Ο χρήστης αυτός δεν υπάρχει.";
					else
					{
						ErrorCodes sendPM = PrivateMessage.send(title, body, uid, recipientID);
						
						if(sendPM != ErrorCodes.OK)
						{
							message = "Παρουσιάστηκε πρόβλημα κατα την αποστολή του μηνύματος.";						
						}
					}
				}
				
				if(message.equals(""))
					response.sendRedirect("ViewChat?id=" + recipientID);
				else
				{
					response.setContentType("text/html; charset=UTF-8");	
					PrintWriter out = response.getWriter();
					
					out.println(getPage(uid, recipientID, message));
					
					out.close();					
				}
				
			}
			else
			{
				response.setContentType("text/html; charset=UTF-8");	
				PrintWriter out = response.getWriter();
				
				out.println(getPage(uid, recipientID, "Παρουσιάστηκε πρόβλημα κατα την αποστολή του μηνύματος."));
				
				out.close();					
			}
			
			
		}
	
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			request.setCharacterEncoding("utf-8");
			String recipientID = request.getParameter("id");
			
			if(recipientID == null)
			{
				response.sendRedirect("Profile");				
			}
			else
			{			
				response.setContentType("text/html; charset=UTF-8");	
				PrintWriter out = response.getWriter();
				
				out.println(getPage(uid, recipientID, ""));
				
				out.close();			
			}
		}
	}
}